﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PathManager : MonoBehaviour {

    //Runner variable
    public GameObject runner;
    private GameObject runnerNextGoal;
    private int runnerNextGoalIndex;
    [SerializeField]
    private float runnerSpeed;

    public float RunnerSpeed
    {
        get { return runnerSpeed; }
        set { runnerSpeed = value; }
    }

    private GameObject[] path;
    private List<GameObject> rail;

	// Use this for initialization
	void Start () 
    {
        rail = new List<GameObject>();
        EventManager<Level>.AddListener("set level", OnSetLevel);
        EventManager<EndLevelStat>.AddListener("end level", onLevelEnd);
	}
	
	// Update is called once per frame
	void Update () 
    {
        //The runner follows the path during each game
	    if(runner.activeSelf)
        {
            runner.transform.LookAt(runnerNextGoal.transform.position);
            runner.transform.Translate(Vector3.forward * RunnerSpeed * Time.deltaTime);
            if(Vector3.Distance(runner.transform.position, runnerNextGoal.transform.position) < 0.5)
            {
                runnerNextGoalIndex++;
                if(runnerNextGoalIndex == path.Length)
                {
                    runnerNextGoalIndex = 0;
                    runnerNextGoal = path[1];
                    runner.transform.position = path[runnerNextGoalIndex].transform.position;
                }
                else
                {
                    runnerNextGoal = path[runnerNextGoalIndex];
                }
            }
        }
	}

    public GameObject[] getPath()
    {
        return path;
    }

    //Initialize the path
    public void setPath(GameObject[] newPath)
    {
        clear();
        path = newPath;
        for(int index = 0; index < path.Length - 1; ++index)
        {
            //Create the node on the path
            GameObject tempSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            tempSphere.transform.position = path[index].transform.position;

            //Link the nodes together
            GameObject tempObj = new GameObject();
            LineRenderer lr = tempObj.AddComponent<LineRenderer>();
            lr.SetPosition(0, path[index].transform.position);
            lr.SetPosition(1, path[index +1].transform.position);

            //Keep track of the gameobjects to destroy them later
            rail.Add(tempSphere);
            rail.Add(tempObj);
        }
        //Iniate the runner for the level
        runner.SetActive(true);
        runner.transform.position = path[0].transform.position;
        runnerNextGoal = path[1];
        runnerNextGoalIndex = 1;
    }

    //Clear the path at the end of the level
    public void clear()
    {
        foreach (GameObject obj in rail)
        {
            Destroy(obj);
        }
        runner.SetActive(false);
    }

    private void OnSetLevel(Level nLevel)
    {
        setPath(nLevel.Path);
    }

    private void onLevelEnd(EndLevelStat stat)
    {
        clear();
    }
}
